/**
 * 描述：生命周期模型管理
 * 作者：chenhui
 * 日期：6/21/2019
 * 版本：0.0.1
 **/

(function(E, KIT, RES) {
  "use strict";

  var __URL_ROOT = RES.URL_ROOT + "/lifecycle";
  var __conflict_id, __conflict_node_id;

  var _getForm = function(value, text, css) {
      var _arr_id = value.split(",");
      var list = [];
      var _str = "";
      list = RES.FORMNAME;
      var __len = list.length;
      for(var __i = 0; __i < __len; __i++) {
        if(_arr_id.length == 1) {
          if(list[__i].id == value) {
            return list[__i].name;
          }
        }
        for(var __j = 0; __j < _arr_id.length; __j++) {
          if(list[__i].id == _arr_id[__j]) {
            _str += list[__i].name + ",";
            //return list[__i].name;
          }
        }
      }
      if(_str != "") {
        _str = _str.substring(0, _str.length - 1);
      }
      css.color = 6;
      return _str;
    },

    _getFileType = function(value, text, css) {
      var list = [];
      list = RES.FILETYPE;
      var __len = list.length;
      for(var __i = 0; __i < __len; __i++) {
        if(list[__i].customid == value) {
          return list[__i].codename;
        }
      }
      css.color = 6;
      css.italic = true;
      return "未知类型";
    };

  //region ---------------------------- 表单字段 Grid Fields ----------------------------
  // 生命周期名称
  var __GRID_FIELDFORM = [{
      title: "名称", name: "attrib_01", width: 200,
      editor: {
        validator: {
          rules: {required: true, maxlength: 100}
        }
      }
    }, {
      title: "编号", name: "attrib_02", width: 120,
      editor: {
        validator: {
          rules: {required: true, maxlength: 100}
        }
      }
    }, {
      title: "菜单目录", name: "menu_type", visible: false,
      editor: {
        tip: "请选择...",
        name: "pid",
        type: "picker",
        editable: false,
        dataSource: {
          textField: "name",
          valueField: "id",
          url: __URL_ROOT + "/getMenus"
        }
      }
    }, {
      title: "状态", name: "attrib_15", visible: false,
      editor: {
        type: "radiolist",
        defaultValue: 1,
        dataSource: RES.ARR_STATUS_TYPE
      }
    }, {
      title: '描述', name: 'attrib_19', visible: false, size: 12,
      editor: KIT.tmplWangEditor()
    }],
    // 生命周期节点
    __GRID_FIELDS = [{
      title: "节点名称", name: "attrib_01", width: 150,
      editor: {
        validator: {
          rules: {required: true, maxlength: 100}
        }
      }
    }, {
      title: '关联表单', name: 'attrib_12', visible: false,
      formatter: _getForm,
      editor: {
        name: "attrib_12",
        type: "picker",
        tip: "请选择...",
        autoReload: true,
        editable: true,
        multiple: true,
        dataSource: {
          textField: "name",
          valueField: "id",
          url: __URL_ROOT + "/getFormName"
        },
        autocomplete: {
          paramName: "search_name",
          dataSource: {
            textField: "name",
            valueField: "id",
            url: __URL_ROOT + "/getFormName"
          }
        },
        validator: {
          rules: {
            required: true
          }
        }
      }
    }, {
      title: "表单名称", name: "attrib_13", width: 150
    },{
        title: '关联标签', name: 'attrib_20', visible: true,width: 200,

        editor: {
            name: "attrib_20",
            type: "picker",
            tip: "请选择...",
            autoReload: true,
            editable: true,
            multiple: true,
            dataSource: {
                textField: "name",
                valueField: "name",
                url: RES.URL_ROOT + "/lifecycexample/getTabName"
            },
            autocomplete: {
                paramName: "search_name",
                dataSource: {
                    textField: "name",
                    valueField: "name",
                    url: RES.URL_ROOT + "/lifecycexample/getTabName"
                }
            }/*,
            validator: {
                rules: {
                    required: true
                }
            }*/
        }
    }, {
      title: "顺序编号", name: "attrib_11", width: 150,
      editor: {
        validator: {
          rules: {
            required: true,
            numeric: true,
            min: 1,
            max: 1000000
          }
        }
      }
    }, {
      title: "状态", name: "attrib_15", align: "center", width: 150,
      formatter: function(value, text, css) {
        css.color = value == 1 ? null : 6;
        return value == 1 ? "启用" : "禁用";
      },
      editor: {
        type: "radiolist",
        defaultValue: 1,
        dataSource: RES.ARR_STATUS_TYPE
      }
    }],
    // 生命周期节点附件
    __GRID_FIELDSS = [{
      title: "附件类型", name: "attrib_01", width: 200,
      formatter: _getFileType,
      editor: {
        name: "attrib_01",
        type: "picker",
        tip: "请选择...",

        dataSource: {
          textField: "codename",
          valueField: "customid",
          url: __URL_ROOT + "/getFileType"
        }
      }
    }, {
      title: "是否必传", name: "attrib_15", width: 150, align: "center",
      formatter: function(value, text, css) {
        css.color = value == 1 ? 1 : null;
        return value == 1 ? "是" : "否";
      },
      editor: {
        type: "radiolist", defaultValue: 1,
        dataSource: [{text: "是", value: 1}, {text: "否", value: 0}]
      }
    }],
    _getGridSingeActions_ = [{
      "创建菜单": function(e) {
        _createMenu(rapid("#lstForm").grid().selection()[0]);
      }
    }],
    _initMenu_ = function() {
      rapid("#frmMenu").dataview({
        colSize: 1,
        fields: [{
          title: "菜单", name: "menu_type", width: 220, editor: {
            name: "pid",
            type: "picker",
            tip: "请选择...",
            editable: false,
            dataSource: {
              textField: "name",
              valueField: "id",
              url: __URL_ROOT + "/getMenus"
            },
            validator: {
              rules: {required: true}
            }
          }
        }]
      });
    },
    _createMenu = function(__slt) {
      var __grid = rapid("#lstForm").grid();
      rapid("#wndMenu").window({
        title: "创建菜单",
        size: "xs",
        buttons: [{
          "保存": function(e) {
            var s = this;
            var __dataview = rapid("#frmMenu").dataview();

            if(!__dataview.valid()) {
              return;
            }
            rapid.confirm("确定要保存吗？", function(e) {
              var data = __dataview.serializeJson();
              data.exampleName = __slt.ui("attrib_01").val();
              data.exampleCode = __slt.ui("attrib_02").val();
              if(e) {
                rapid.post({
                  id: "saveMenu",
                  url: __URL_ROOT + "/saveMenu",
                  data: data,
                  success: function(result) {
                    s.hide();
                    __grid.reload();
                  }
                });
              }
            });
          }
        }],
        onHide: function() {
          rapid("#frmMenu").dataview().reset();
        }
      }).show();
    };
  //endregion ------------------------- Grid 字段 ----------------------------

  //region ---------------------------- 表单方法 Grid Method ----------------------------
  //生命周期模型
  var _addOrUpdate = function(e) {
    var __grid = this;

    rapid.confirm("确定要保存吗？", function(yes) {
      if(yes) {
        rapid.post({
          url: __URL_ROOT + "/saveLifecycModel",
          data: e.serializeJson(),
          success: function(result) {
            e.hide();
            rapid.success("保存成功。", function() {
              __grid.reload();
            }, false);
          }
        });
      }
    });
  };
  //生命周期模型节点
  var _addOrUpdateNode = function(e) {
    var __grid = this;
    rapid.confirm("确定要保存吗？", function(yes) {
      if(yes) {
        rapid.post({
          url: __URL_ROOT + "/saveLifecycModelNode",
          data: $.extend(true, {}, e.serializeJson(), {row_id: __conflict_id}),
          success: function(result) {
            e.hide();
            rapid.success("保存成功。", function() {
              __grid.reload();
            }, false);
          }
        });
      }
    });
  };
  //生命周期模型节点附件
  var _addOrUpdateNodeAcc = function(e) {
    var __grid = this;
    rapid.confirm("确定要保存吗？", function(yes) {
      if(yes) {
        rapid.post({
          url: __URL_ROOT + "/saveLifecycModelNodeAcc",
          data: $.extend(true, {}, e.serializeJson(), {row_id: __conflict_node_id, attrib_02: __conflict_id}),
          success: function(result) {
            e.hide();
            rapid.success("保存成功。", function() {
              __grid.reload();
            }, false);
          }
        });
      }
    });
  };
  //endregion ------------------------- Grid 方法 ----------------------------

  // 生命周期名称
  var _initGridList = function() {
    var gird = rapid("#lstForm").grid({
      height: "100%",
      addable: true,
      updatable: true,
      removable: true,
      detailed: true,
      quickView: false,
      dataSource: {
        url: __URL_ROOT + "/getLifecycModel"
      },
      dataview: {
        colSize: 2
      },
      window: {
        size: "xl"
      },
      pageable: true,
      keyFields: "conflict_id",
      paramFields: ["conflict_id"],
      fields: __GRID_FIELDFORM,
      singleActions: _getGridSingeActions_
    });

    gird
      .on("startselect", function() {
        rapid("#grd").grid().disabled(true);
      })
      .on("endselect", function() {
        rapid("#grd").grid().disabled(false);
      })
      .on("loadsuccess", function() {
        rapid("#grd").grid().clear().disabled(false).endSelect();
        rapid("#grds").grid().clear().disabled(false).endSelect();
      })
      .on("click", function(e) {
        __conflict_id = this.current().ui().conflict_id.val();

        rapid("#grd").grid().load({
          data: "row_id=" + __conflict_id
        }).disabled(false).endSelect();

        rapid("#grds").grid().clear().disabled(false).endSelect();
      })
      .on("add", _addOrUpdate)
      .on("update", _addOrUpdate)
      .on("remove", function(ids) {
        var __grid = this;
        rapid.post({
          url: __URL_ROOT + "/deleteLifecycModel",
          data: "ids=" + ids,
          success: function() {
            __grid.reload();
            rapid("#grd").grid().clear();
            rapid("#grds").grid().clear();
          }
        });
      });
  };

  // 生命周期节点
  var _initGrid = function() {
    rapid("#grd").grid({
                   height: "100%",
                   addable: true,
                   updatable: true,
                   removable: true,
                   detailed: true,
                   dataSource: {
                     url: __URL_ROOT + "/getLifecycModelNode"
                   },
                   pageable: true,
                   keyFields: "conflict_id",
                   paramFields: ["conflict_id"],
                   fields: __GRID_FIELDS
                 })
                 .on("startselect", function() {
                   rapid("#grds").grid().disabled(true);
                 })
                 .on("endselect", function() {
                   rapid("#grds").grid().disabled(false);
                 })
                 .on("loadsuccess", function() {
                   rapid("#grds").grid().clear().disabled(false).endSelect();
                 })
                 .on("click", function(e) {
                   __conflict_node_id = this.current().ui().conflict_id.val();

                   rapid("#grds").grid().load({
                     data: "row_id=" + __conflict_node_id
                   }).disabled(false).endSelect();
                 })
                 .on("add", _addOrUpdateNode)
                 .on("beforeAdd", function() {
                   var _slt = rapid("#lstForm").grid().selection()[0];
                   if(_slt == null) {
                     rapid.warn("请选择生命周期名称！");
                     return false;
                   }
                 })
                 .on("update", _addOrUpdateNode)
                 .on("remove", function(ids) {
                   var __grid = this;
                   rapid.post({
                     url: __URL_ROOT + "/deleteLifecycNode",
                     data: "ids=" + ids,
                     success: function() {
                       __grid.reload();
                       rapid("#grds").grid().clear();
                     }
                   });
                 });
  };

  // 生命周期节点附件
  var _initGrids = function() {
    rapid("#grds").grid({
                    height: "100%",
                    addable: true,
                    updatable: true,
                    removable: true,
                    detailed: true,

                    dataSource: {
                      url: __URL_ROOT + "/getLifecycAcc"
                    },
                    pageable: true,
                    keyFields: "conflict_id",
                    paramFields: ["conflict_id"],
                    fields: __GRID_FIELDSS
                  })
                  .on("add", _addOrUpdateNodeAcc)
                  .on("beforeAdd", function() {
                    var _slt = rapid("#grd").grid().selection()[0];
                    if(_slt == null) {
                      rapid.warn("请选择生命周期节点！");
                      return false;
                    }
                  })
                  .on("update", _addOrUpdateNodeAcc)
                  .on("remove", function(ids) {
                    var __grid = this;
                    rapid.post({
                      url: __URL_ROOT + "/deleteLifecycAcc",
                      data: "ids=" + ids,
                      success: function() {
                        __grid.reload();
                      }
                    });
                  });
  };

  rapid.Lifecycle(function() {
    _initGridList();
    _initGrid();
    _initGrids();
    _initMenu_();

    rapid("#lstForm").grid().load();

  }).init();

})
(rapid.html.Element, rapid.kit, rapid.res);
